﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.DS
{
    public static partial class experiment
    {
        public static dynamic swapEOrder(bool flags,int eid)
        {
            var sql = new Sql.SqlDataContext();
            var current = sql.DSExperiment.FirstOrDefault(row => row.eid == eid);
            if(current == null) return new{
                success = false,
                message = "找不到当前Experiment"
            };
            if (flags == true)                                                      // Up
            {
                if (!sql.DSExperiment.Any(row => row._order < current._order))
                    return new
                    {
                        success = true
                    };
                var max = (from mm in sql.DSExperiment
                           where mm._order < current._order
                           orderby mm._order descending
                           select mm).FirstOrDefault();
                current._order ^= max._order;
                max._order ^= current._order;
                current._order ^= max._order;
            }
            if (flags == false)                                                      // Down
            {
                if (!sql.DSExperiment.Any(row => row._order > current._order))
                    return new
                    {
                        success = true
                    };
                var min = (from mm in sql.DSExperiment
                           where mm._order > current._order
                           orderby mm._order
                           select mm).FirstOrDefault();
                current._order ^= min._order;
                min._order ^= current._order;
                current._order ^= min._order;
            }
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
